window.onload = function () {
    window.vue = new Vue({
        el: '#my-div',
        data: {
            list: [],
            menuList: [],
            selectedMenuNames: '',
            name: '',
            menus: [],
            id: '',
            index: '',
        },
        methods: {
            find: function () {
                doGet({
                    url: 'user/role/query',
                    data: {},
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.list = res.data.data;
                            $('#table-main')[0].style.visibility = 'visible';
                        } else {
                            console.log(res);
                        }
                    }
                });
            },

            queryAllMenu: function () {
                doGet({
                    url: 'user/menu/query',
                    data: {},
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.menuList = res.data.data;
                            $('#table-menu')[0].style.visibility = 'visible';
                        } else {
                            console.log(res);
                        }
                    }
                });
            },

            del: function (id) {
                window.parent.showConfirm('重要提示', '您确定要删除该角色吗?', function () {
                    doPost({
                        url: 'user/role/delete',
                        data: {
                            id: id,
                        },
                        callback: function (res) {
                            if (res.data.code === 1) {
                                vue.list.removeAt(vue.index);
                                vue.list = vue.list.clone();
                                window.parent.showSuccess();
                            } else {
                                window.parent.showInfo(res.data.msg);
                            }
                        }
                    });
                });

            },

            showUpdate: function (i) {
                if (i > -1) {
                    this.menus = this.list[i].menus;
                    this.name = this.list[i].name;
                    this.id = this.list[i].id;
                    this.index = i;
                    this.selectedMenuNames = JSON.stringify(this.list[i].menus);
                }
                $('#update-modal').modal('show');
                setTimeout(function () {
                    updateCheckboxStyle();
                }, 200)
            },

            save: function () {
                let menus = [];
                let cks = $('input.flat-red').iCheck('update');
                for (let i = 0; i < this.menuList.length; i++) {
                    let input = cks[i];
                    if (input.checked) {
                        menus.push(this.menuList[i].id)
                    }
                }
                menus = JSON.stringify(menus);

                doPost({
                    url: 'user/role/update',
                    data: {
                        id: vue.id,
                        name: vue.name,
                        menus: menus,
                    },
                    callback: function (res) {
                        if (res.data.code === 1) {
                            vue.find();
                            window.parent.showSuccess();

                            $('#add-modal').modal('hide');
                            $('#update-modal').modal('hide');

                            this.id = '';
                            this.name = '';
                            this.menus = [];
                        } else {
                            window.parent.showInfo(res.data.msg);
                        }
                    }
                });
            },
        },
    });
    vue.find();
    vue.queryAllMenu();

    $('#inputRole').bind('keypress', function (event) {
        if (event.keyCode === 13) {
            vue.save();
        }
    });

};
